package ecstore.client.customWidgets;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

import ecstore.client.DataStorageService;
import ecstore.client.DataStorageServiceAsync;
import ecstore.client.dataOperation.ItemInfo;

//import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;


public class ItemEditor extends Composite {
	// Create the popup dialog box
//	final DialogBox dialogBox = new DialogBox();
	final Button closeButton = new Button("Close");
	
	VerticalPanel verticalPanel = new VerticalPanel();
	ItemEditorPanel itemEditorPanel;
	
	private final DataStorageServiceAsync dataStorageService = GWT.create(DataStorageService.class);
	
	public ItemEditor(final ItemInfo itemInfo) {
//		dialogBox.setSize("0", "318");
		
//		dialogBox.setText("Item Editor");
//		dialogBox.setAnimationEnabled(true);
//		dialogBox.setWidget(verticalPanel);
		verticalPanel.setSize("800px", "318px");
		
		initWidget(verticalPanel);
		
		HorizontalSplitPanel horizontalSplitPanel = new HorizontalSplitPanel();
		horizontalSplitPanel.setSize("790px", "381px");
		verticalPanel.add(horizontalSplitPanel);
		
		Image image = new Image((String) null);
		horizontalSplitPanel.setLeftWidget(image);
		image.setSize("100%", "100%");
		
		itemEditorPanel = new ItemEditorPanel();
		itemEditorPanel.setPanel(itemInfo);
		horizontalSplitPanel.setRightWidget(itemEditorPanel);
		itemEditorPanel.setSize("100%", "100%");
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(horizontalPanel);
		verticalPanel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel.setWidth("380px");
		
		Button btnUpdate = new Button("New button");
		btnUpdate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ItemInfo newItemInfo = itemEditorPanel.getItemInfo();
				System.out.println("starting update");
				dataStorageService.updateItem(itemInfo.getKeyString(), newItemInfo, new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("failed to update the item");
					}

					@Override
					public void onSuccess(Boolean result) {
						System.out.println("updated the item");
					}
					
				});
			}
		});
		btnUpdate.setText("Update");
		horizontalPanel.add(btnUpdate);
		
		Button btnCancel = new Button("New button");
		btnCancel.setText("Cancel");
		horizontalPanel.add(btnCancel);
		
		horizontalPanel.add(closeButton);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				verticalPanel.removeFromParent();
//				dialogBox.hide();
			}
		});
		
//		dialogBox.center();
	}

}
